﻿Imports System.Web.UI

''' <summary>
''' Provides extension methods to the <see cref="System.Web.UI">System.Web.UI</see> object.
''' </summary>
''' <revisions>
'''   <revision date="11/02/2008" author="Chad Green" version="1.0.0.0">Initial Development</revision>
''' </revisions>
Public Module WebControlExtensions

  ''' <summary>
  ''' Extension method to recursively look for the specified control.
  ''' </summary>
  ''' <param name="root">The root object where to start from.</param>
  ''' <param name="ID">Identifier of the control to look for.</param>
  ''' <returns>A reference to the specified <see cref="Control">Control</see>.</returns>
  ''' <example>
  '''   <para>The following examples call a process method if the specified checkbox is checked.</para>
  '''   <code language="vb">
  '''     Private Sub ProcessButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  '''       If CType(Page.FindControlR("chkSaveFile"), CheckBox).Checked = True Then
  '''        CaseFile.Process(hidCaseFileID.Value.ToInt32)
  '''      End If
  '''    End Sub
  '''   </code>
  '''   <code language="c#">
  '''     private void ProcessButton_Click(object sender, System.EventArgs e)
  '''     {
  '''     	if (((CheckBox)Page.FindControlR("chkSaveFile")).Checked == true)
  '''     	{
  '''     		CaseFile.Process(hidCaseFileID.Value.ToInt32);
  '''     	}
  '''     }
  '''   </code>
  ''' </example>
  ''' <revisions>
  '''   <revision date="11/02/2008" author="Chad Green" version="1.00.000">Initial Development</revision>
  ''' </revisions>
  <Extension()> Public Function FindControlR(ByVal root As Control, ByVal ID As String) As Control

    ' Variable Declarations
    Dim objControlFound As Control = Nothing

    ' Find the specified control
    If IsNothing(root) = False Then
      objControlFound = root.FindControl(ID)
      If IsNothing(objControlFound) = True Then
        For Each objControl As Control In root.Controls
          objControlFound = objControl.FindControlR(ID)
          If IsNothing(objControlFound) = False Then
            Exit For
          End If
        Next objControl
      End If
    Else
      objControlFound = Nothing
    End If

    ' Return the results
    Return objControlFound

  End Function

End Module